package com.markhsiu.MiniMQ.remote.transport.netty;

import com.markhsiu.MiniMQ.broker.store.StoreManager;
import com.markhsiu.MiniMQ.channel.ChannelProcessor;
import com.markhsiu.MiniMQ.model.Message;
import com.markhsiu.MiniMQ.model.ResponseMessage;
import com.markhsiu.MiniMQ.model.constant.MessageCmdEnum;
import com.markhsiu.MiniMQ.model.constant.MessageSourceEnum;
import com.markhsiu.MiniMQ.remote.AbstractMessageProcessor;

public class NettyProducerMessageProcessor extends AbstractMessageProcessor{
  
	@Override
	public void handler(final Message request,ChannelProcessor channel) {
 
		if(checkMassage(request)){
			channel.writeAndFlush(ResponseMessage.newError());
			return;
		}
		
		 
		Message response = ResponseMessage.newSuccess() ;
		response.setMessageID(request.getMessageID());
		response.setSource(MessageSourceEnum.BROKER);
		response.setTarget(MessageSourceEnum.PRODUCER);
		switch (request.getCmd()) {
			case CONNECT:	
				break;
			case DISCONNECT:	
				break;
			case NEW:
				request.setTimestamp(System.currentTimeMillis());
				request.setCmd(MessageCmdEnum.ING);
				StoreManager.addMessage(request);
				break;
			default:
				response.setCmd(MessageCmdEnum.FAIL);
				break;
		}
	 
		channel.writeAndFlush(response);
	}
}
